<?php

/**
 * Description of SupportController
 *
 * @author Poh Nean
 */
class SupportController extends PsController
{
	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
					)), parent::accessRules());
	}
	
    public function actionIndex()
    {

        $supportForm = new SupportForm;

        if ($_POST["SupportForm"]) {
            $supportForm->attributes = $_POST["SupportForm"];

            $isValid = $supportForm->validate();

            $user = Yii::app()->user->model;

            if ($isValid) {
                $mailer = new Mailer();
                $mailer->layout = Yii::getPathOfAlias('common') . "/templates/emails/layouts/default.tpl";
                $mailer->Subject = 'New Support Ticket: ' . $supportForm->type;
                $mailer->From = Yii::app()->params["adminEmail"];
                $mailer->FromName = Yii::app()->params["adminName"];
                $mailer->ReplyTo = $user->email;

                $mailer->AddAddress(Yii::app()->params["supportEmail"], Yii::app()->params["supportName"]);

                $tokens = array(
                    'message' => $supportForm->message,
                    'type' => $supportForm->type,
                    'userEmail' => $user->email,
                    'name' => $user->name,
					'username' => $user->username,
					'mobile' => $user->mobile
                );
                
                $mailer->createHTMLMail(Yii::getPathOfAlias('common') . "/templates/emails/support/html/enquiry.tpl", $tokens);
                $mailer->createTEXTMail(Yii::getPathOfAlias('common') . "/templates/emails/support/text/enquiry.tpl", $tokens);

                if ($mailer->sendMail()) {
                    PsSystemMessage::add("Enquiry submitted. We'll respond to you as soon as possible.", "success");
                } else {
                    PsSystemMessage::add("Error submitting enquiry.", "error");
                }
            }
        }

        $data = array();
        $data["model"] = $supportForm;

        $this->render('index', $data);
    }

}

?>
